#!/bin/env python

import os
import ConfigParser

import database

class Db():
    def __init__(self,db_config,db_section="insert_db"):
        self.init(db_config,db_section)
        pass


    def init(self,db_config,db_section="insert_db"):
        (host,port,user,password,db) = self.readDbConfig(db_config,db_section)
        self.db = database.Connection("%s:%s"%(host,port),db,user,password)

    def readDbConfig(self,db_config,db_section):
        cf = ConfigParser.ConfigParser()
        if not os.path.exists(db_config): 
            print '''>>sys.stderr,'''"%s not exist!"%db_config
            os._exit(-1)
        cf.read(db_config)
        return cf.get(db_section,"host"), \
            cf.get(db_section,"port"), \
            cf.get(db_section,"user"), \
            cf.get(db_section,"password"), \
            cf.get(db_section,"db") 

    def getDb(self):
        if self.db:
            return self.db
        return None

    def test(self):
        for files in self.db.iter("select * from clnd_imei limit 10"):
            print files

    def select(self,select_value,table):
        select_where = ""
        select_list = []
        for key in select_value:
            select_where += key+"="+"%s"+" and "
            select_list.append(select_value[key])
        select_where = select_where[:-5]
        query="select * from %s where %s;"%(table,select_where)
        #print query
        return self.db.get(query,*select_list)

    def update(self,select_value,update_value,table):
        update_list = []
        update_condition = ""
        select_where = ""
        select_list = []
        for key in update_value:
            update_condition += key+"="+"%s"+","
            update_list.append(update_value[key])
        for key in select_value:
            select_where += key+"="+"%s"+" and "
            update_list.append(select_value[key])
        select_where = select_where[:-5]
        update_condition = update_condition[:-1]
        update_query="update %s set %s where %s"%(table,update_condition,select_where)
        self.db.execute(update_query,*update_list)


    def insert(self,select_value,insert_value,table):
        insert_list = []
        field = ""
        values = ""
        for key in select_value:
            field += key+","
            values += "%s"+","
            insert_list.append(select_value[key])
        for key in insert_value:
            field += key+","
            values += "%s"+","
            insert_list.append(insert_value[key])
        field = field[:-1]
        values = values[:-1]
        insert_query="insert into %s(%s) values(%s);"%(table,field,values)
        self.db.execute(insert_query,*insert_list) 


    def insertExample(self,select_value,insert_value,table):
        select_where = ""
        select_list = []
        for key in select_value:
            select_where += key+"="+"%s"+" and "
            select_list.append(select_value[key])
        select_where = select_where[:-5]
        query="select * from %s where %s;"%(table,select_where)
        if self.db.get(query,*select_list)!= None:
            update_list = []
            update_condition = ""
            for key in insert_value:
                update_condition += key+"="+"%s"+","
                update_list.append(insert_value[key])
            for item in select_list:
                update_list.append(item)
            update_condition = update_condition[:-1]
            update_query="update %s set %s where %s"%(table,update_condition,select_where)
            self.db.execute(update_query,*update_list)
        else:
            insert_list = []
            field = ""
            values = ""
            for key in select_value:
                field += key+","
                values += "%s"+","
                insert_list.append(select_value[key])
            for key in insert_value:
                field += key+","
                values += "%s"+","
                insert_list.append(insert_value[key])

            field = field[:-1]
            values = values[:-1]
            insert_query="insert into %s(%s) values(%s);"%(table,field,values)
            self.db.execute(insert_query,*insert_list) 
        



